package core.base.controller;

import java.util.ArrayList;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import core.json.JSONError;
import core.json.JSONResponse;

public class JSONController extends BaseSpringController {
	protected Validator validator;

	public JSONController(Validator validator) {
		this.validator = validator;
	}
	
	public JSONResponse successed(Object obj) {
		JSONResponse ret = new JSONResponse();
		ret.setSuccessed(true);
		ret.setReturnObject(obj);
		
		return ret;
	}
	
	public JSONResponse failed(Set failures) {
		Set<ConstraintViolation<?>> failureSet = (Set<ConstraintViolation<?>>)failures;
		ArrayList<JSONError> errors = new ArrayList<JSONError>();
		for (ConstraintViolation<?> failure : failureSet) {
			errors.add(new JSONError(failure.getPropertyPath().toString(), failure.getMessage()));
		}
		
		JSONResponse ret = new JSONResponse();
		ret.setSuccessed(false);
		ret.setErrors(errors);
		
		return ret;
	}
}